﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SCRUM.Core.BusinessObjects;
using SCRUM.Core.DAO;

/// <summary>
/// Handles operations on projects, create, edit show and delete
/// </summary>
public partial class ProjectDetails : HierarchicalObjectDetails<Project,object>
{
    /// <summary>
    /// Load page method 
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void Page_Load(object sender, EventArgs e)
    {
        this.LoadExtendedInputsFrom(PanelItemDetails);
    }

    protected override IList<ScrumInput> LoadExtendedInputs()
    {
        return this.LoadExtendedInputsFrom(PanelItemDetails);
    }

    /// <summary>
    /// does nothing
    /// </summary>
    protected override void LoadAvailableSuperiorItems() { }

    /// <summary>
    /// Displays all fields of the selected item into textboxes in form.
    /// </summary>
    public override void DisplayItem(Project project)
    {
        this.Visible = true;
        
        bool exists = project != null;

        // values
        this.Header.LastChange =        exists ? project.LastChange     : DateTime.MinValue;
        this.Name.Text =                exists ? project.Name           : "";
        this.Description.Text =         exists ? project.Description    : "";
        this.Customer.Text =            exists ? project.Customer       : "";
        this.StartDate.SeledtedDate =   exists ? project.StartDate      : DateTime.Today;
        this.States.SelectedState =     exists ? project.State          : State.DefaultState(State.StateTypes.Project);
        this.Progress.Progress =        exists ? project.Progress       : 0;
        this.Estimation.Value =         exists ? project.Estimation     : 0;

        // enabled
        States.Enabled = exists;

        // visibility
        Progress.Visible = exists;
        //Estimation.Visible = exists;

        //readonly
        //Estimation.ReadOnly = exists;
    }

    /// <summary>
    /// Creates new project
    /// </summary>
    /// <returns>Created project</returns>
    public override Project CreateItem()
    {
        Project proj = new Project(
            0, 
            States.SelectedState.ID, 
            Name.Text,
            Description.Text,
            Estimation.Value,
            0,
            StartDate.SeledtedDate.Value,
            Customer.Text,
            DateTime.Now
        );
        Project newProj = DAOProject.CreateNewProject(proj);
        return newProj;
    }

    /// <summary>
    /// Deletes project
    /// </summary>
    /// <param name="project">Project to delete</param>
    public override void DeleteItem(Project proj)
    {
        DAOProject.DeleteProject(proj.ID);
        this.Visible = false;
    }

    /// <summary>
    /// Edits project according to data in project detail panel
    /// </summary>
    /// <param name="oldProj">Project to update</param>
    public override Project EditItem(int oldProjId)
    {
        Project proj = new Project(
            oldProjId, 
            States.SelectedState.ID,
            Name.Text,
            Description.Text,
            Estimation.Value, 0,
            StartDate.SeledtedDate.Value,
            Customer.Text,
            DateTime.Now
        );
        DAOProject.UpdateProject(proj);
        this.Visible = false;
        DisplayItem(proj);
        return proj;
    }

    /// <summary>
    /// Validates the input.
    /// </summary>
    /// <returns></returns>
    public override bool ValidateInput()
    {
        throw new NotImplementedException();
        return true;
    }

    protected override Panel panelWithControls { get { return this.PanelItemDetails; } }
}
